var localObj = window.location;
var basePath = localObj.protocol+"//"+localObj.host;
var modelAddModule=angular.module('userAddApp', []);
    modelAddModule.controller('userAddCtr',['$scope', '$http',function($scope, $http) {
		// 模型名称校验默认不显示
		$scope.checkUserAccount = false;
		
		// 点击保存按钮，新增一条数据
		$scope.saveUser = function() {
			// form表单的验证 数据不对，return false
			var formValid = $scope.userForm.$valid;
				if (!$scope.userName) {
					toastr.warning("用户名称不可为空！");
					formValid = false;
					return false;
				}else if (!$scope.userPassword) {
					toastr.warning("密码不可为空！");
					formValid = false;
					return false;
				}else if (!$scope.userType) {
					toastr.warning("用户类型不可为空！");
					formValid = false;
					return false;
				}else if (!$scope.organizeName) {
					toastr.warning("组织名称不可为空！");
					formValid = false;
					return false;
				}else if (!$scope.userPhone) {
					toastr.warning("联系电话不可为空！");
					formValid = false;
					return false;
				}else if (!$scope.type) {
					toastr.warning("用户级别不可为空！");
					formValid = false;
					return false;
				}/*else if ($scope.organizeId) {
					toastr.warning("组织不能为空！");
					formValid = false;
					return false;
				}*/
//				else if (!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(userPhone))) {
//					toastr.warning("请填写正确电话！");
//					formValid = false;
//					return false;
//				}
				else if (!$scope.userEmail) {
					toastr.warning("邮箱不可为空！");
					formValid = false;
					return false;
				}
//				else if (!( /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/.test(userEmail))) {
//					toastr.warning("请填写正确邮箱！");
//					formValid = false;
//					return false;
//				}
				else if (!$scope.linkMan) {
					toastr.warning("联系人不可为空！");
					formValid = false;
					return false;
				}
				else if ($("#upname").val() == '') {
					toastr.warning("部门名称不可为空！");
					formValid = false;
					return false;
				}
			if (formValid) { // 表单通过验证后提交
				checkUserAccount(); // 校验属性名称是否重复
			}
		};

		function checkUserAccount() {
			var result = true;
			$http({
				url : basePath+'/user/sameUserAccountValidate',
				method : 'POST',
				params : {
					'userName' : $scope.userName
				}
			}).success(function(data, header, config, status) {
				if(data.length==0){
					submitData();
				}else{
					$scope.checkUserAccount = true;
					return;
				}
			}).error(function(data, header, config, status) {
				// 处理响应失败
				result = false;
			});
		}
		// form表单提交方法 submit(若是校验都通过啦，数据便可以提交啦)
		function submitData() {
			
			$http({
				url : basePath+'/user/saveUser',
				method : 'POST',
				params : {
					'userName'  : $scope.userName,//用户名
					'userPassword'  : $scope.userPassword,// 密码
					'userType'  : $scope.userType,// 用户类型
					'organizeName' : $scope.organizeName,
					'province'   :  $scope.province,
					'city' : $scope.city,
					'county' : $scope.county,
					'organizeId' : $scope.organizeId,
					'userPhone' : $scope.userPhone,
					'userEmail' : $scope.userEmail,
					'linkMan' : $scope.linkMan,
					'type' : $scope.type,
					'memberId' : $scope.memberId
				}
			}).success(function(data, header, config, status) {
				toastr.success("操作成功");
				setTimeout(function() {
					location.href = basePath+'/user/user_apply';
				}, 500);
			}).error(function(data, header, config, status) {
				toastr.error("调用错误！");
				setTimeout(function() {
					//location.href = basePath+'/system/sysUser/user_apply.jsp';
				}, 500);
			});
		}
		//  点击取消按钮，跳转到查询页面
		$scope.backUser = function() {
			//location.href = basePath+'/system/sysUser/user_apply.jsp';
		};
}]);
    $(document).ready(function() { 
    	
    $('#province').change(function(){ 
    	var objSelect=$('#type')[0];
    	for (var i = 0; i < objSelect.options.length; i++) { 
    		if (objSelect.options[i].text == "请选择") { 
    		objSelect.options[i].selected = true; 
    		} 
    	}
    	
    	$("#memberId").empty();
		$("#memberId").append("<option value=''>请选择</option>");
		$("#organizeId").empty();
		$("#organizeId").append("<option value=''>请选择</option>");
		var districtCode=$(this).children('option:selected').val(); 
		$.ajax({
			type : "post",
			dataType : "json",
			url : basePath+"/district/district",
			data : {parentCode:districtCode},
			success : function(data) {
				if(data.length>0){
					$("#city").empty();
					$("#city").append("<option value=''>请选择</option>");
					$("#county").empty();
					$("#county").append("<option value=''>请选择</option>");
					for(var i=0;i<data.length;i++){
						$("#city").append("<option value="+data[i].code+">"+data[i].name+"</option>");
					}
				}
			},
			error:function(XMLHttpRequest, textStatus, errorThrown) {
				
			}
		 });
		//查询组织函数
		findOrg(districtCode);
	}); 
	
	$('#city').change(function(){ 
		var districtCode=$(this).children('option:selected').val(); 
		var objSelect=$('#type')[0];
    	for (var i = 0; i < objSelect.options.length; i++) { 
    		if (objSelect.options[i].text == "请选择") { 
    		objSelect.options[i].selected = true; 
    		} 
    	}
    	$("#memberId").empty();
		$("#memberId").append("<option value=''>请选择</option>");
		$("#organizeId").empty();
		$("#organizeId").append("<option value=''>请选择</option>");
		$("#county").empty();
		$("#county").append("<option value=''>请选择</option>");
		$.ajax({
			type : "post",
			dataType : "json",
			url : basePath+"/district/district",
			data : {parentCode:districtCode},
			success : function(data) {
				if(data.length>0){
					for(var i=0;i<data.length;i++){
						$("#county").append("<option value="+data[i].code+">"+data[i].name+"</option>");
					}
					
				}
			},
			error:function(XMLHttpRequest, textStatus, errorThrown) {
				
			}
		 }); 
		//查询组织函数
		findOrg(districtCode);
	}); 
	
	$('#county').change(function(){ 
		var districtCode=$(this).children('option:selected').val(); 
		var objSelect=$('#type')[0];
    	for (var i = 0; i < objSelect.options.length; i++) { 
    		if (objSelect.options[i].text == "请选择") { 
    		objSelect.options[i].selected = true; 
    		} 
    	}
    	$("#memberId").empty();
		$("#memberId").append("<option value=''>请选择</option>");
		$("#organizeId").empty();
		$("#organizeId").append("<option value=''>请选择</option>");
		
		//查询组织函数
		findOrg(districtCode);
	}); 
	
	//查询组织函数
	function findOrg(districtCode){
		$.ajax({
			type : "post",
			dataType : "json",
			url : basePath+"/user/getOrg",
			data : {districtCode:districtCode},
			success : function(data) {
				if(data.length>0){
					$("#organizeId").empty();
					$("#organizeId").append("<option value=''>请选择</option>");
					for(var i=0;i<data.length;i++){
						$("#organizeId").append("<option value="+data[i].id+">"+data[i].organizeName+"</option>");
					}
				}
			}
		});
	}
	
	$('#type').change(function(){ 
		var type=$(this).children('option:selected').val(); 
		var province=$("#province").children('option:selected').val();
		var city=$("#city").children('option:selected').val();
		var county=$("#county").children('option:selected').val();
		var districtCode="";
		if(province!=""){
			districtCode=province;
		}
		if(city!=""){
			districtCode=city;
		}
		if(county!=""){
			districtCode=county;
		}
		if(districtCode==""){
			toastr.warning("请先选择区域！");
			return false;
		}
		if(type=='1'){
			$("#memberId").empty();
			$("#memberId").append("<option value=''>请选择</option>");
		}else if(type=='2'){
			$.ajax({
				type : "post",
				dataType : "json",
				url : basePath+"/district/member",
				data : {districtCode:districtCode,memberType:1},
				success : function(data) {
					$("#memberId").empty();
					$("#memberId").append("<option value=''>请选择</option>");
					if(data.length>0){
						for(var i=0;i<data.length;i++){
							$("#memberId").append("<option value="+data[i].id+">"+data[i].memberName+"</option>");
						}
					}
				},
				error:function(XMLHttpRequest, textStatus, errorThrown) {
					
				}
			 });
		}else if(type=='3'){
			
			$.ajax({
				type : "post",
				dataType : "json",
				url : basePath+"/district/district",
				data : {parentCode:districtCode},
				success : function(data) {
					$("#memberId").empty();
					$("#memberId").append("<option value=''>请选择</option>");
					if(data.length>0){
						for(var i=0;i<data.length;i++){
							$("#memberId").append("<option value="+data[i].code+">"+data[i].name+"</option>");
						}
					}
				},
				error:function(XMLHttpRequest, textStatus, errorThrown) {
					
				}
			 });
		}
	});
    });